/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SelectInventaris.java
 *
 * Created on Nov 22, 2012, 6:51:49 PM
 */
package sistemperpustakaan.view.transaksi;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import sistemperpustakaan.controller.TransaksiController;
import sistemperpustakaan.entity.ViewInventaris;
import sistemperpustakaan.model.InventarisModel;
import sistemperpustakaan.model.TransaksiModel;

/**
 *
 * @author EKo
 */
public class SelectInventaris extends javax.swing.JDialog implements ListSelectionListener {

//    TransaksiModel model;
    InventarisModel model;
    SelectInventarisTableModel tableModel;
    TransaksiController controller;
    String idInventaris = "KI0000000";

    /**
     * Creates new form SelectInventaris
     */
    public SelectInventaris(java.awt.Frame parent, boolean modal, TransaksiController controller) {
        super(parent, modal);
        initComponents();
        this.controller = controller;
        model = new InventarisModel();
//        find(txtCari.getText());
        tableModel = new SelectInventarisTableModel(model.getListInventaris("KI0000000", "", "", "", txtCari.getText(), "", "", 0));
        tableInventaris.setModel(tableModel);
        setAction();
        setTableWidth();
        tableInventaris.getSelectionModel().addListSelectionListener(this);


    }

    public final void setAction() {

        buttonFind.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                find(txtCari.getText());
            }
        });
        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        buttonSelect.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                selectInventaris();
                dispose();

            }
        });

    }

    protected final void find(String judul) {
        tableModel = new SelectInventarisTableModel(model.getListInventaris(txtKodeInventaris.getText(), "", "", "", txtCari.getText(), "", "", 0));
        tableInventaris.setModel(tableModel);
        tableInventaris.getColumnModel().getColumn(1).setPreferredWidth(250);
        tableInventaris.getColumnModel().getColumn(2).setPreferredWidth(150);
        tableInventaris.getColumnModel().getColumn(3).setPreferredWidth(150);
    }

    public void selectInventaris() {
        if (idInventaris == "KI0000000") {
            JOptionPane.showMessageDialog(rootPane, "Anda Belum memilih data");
        } else {
            controller.setSelectedInventaris(idInventaris);

        }
    }

    public final void setTableWidth() {
        int[] width = {80, 150, 100, 200};
        for (int i = 0; i < 4; i++) {
            tableInventaris.getColumnModel().getColumn(i).setPreferredWidth(width[i]);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        buttonCancel = new javax.swing.JButton();
        buttonSelect = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableInventaris = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        txtCari = new javax.swing.JTextField();
        buttonFind = new javax.swing.JButton();
        txtKodeInventaris = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        setUndecorated(true);

        jPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        buttonCancel.setText("Cancel");

        buttonSelect.setText("Select");

        tableInventaris.setAutoCreateRowSorter(true);
        tableInventaris.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableInventaris.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        tableInventaris.setRowHeight(22);
        tableInventaris.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tableInventaris.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tableInventaris);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setText("Judul");

        buttonFind.setText("Find");

        jLabel2.setText("Kode Inventaris");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(buttonSelect)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonCancel))
                            .addComponent(jScrollPane1)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtKodeInventaris, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtCari, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(401, 401, 401)
                        .addComponent(buttonFind, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(16, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonFind)
                    .addComponent(txtKodeInventaris, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCari, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonCancel)
                    .addComponent(buttonSelect))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonFind;
    private javax.swing.JButton buttonSelect;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableInventaris;
    private javax.swing.JTextField txtCari;
    private javax.swing.JTextField txtKodeInventaris;
    // End of variables declaration//GEN-END:variables

    public void valueChanged(ListSelectionEvent e) {
        try {
            idInventaris = (String) (tableInventaris.getValueAt(tableInventaris.getSelectedRow(), 0).toString());
        } catch (Exception ex) {
        }
    }
}

class SelectInventarisTableModel extends AbstractTableModel {

    List<ViewInventaris> listInventaris;
    private String[] header = {"ID Inventaris", "Judul", "ISBN", "Pengarang"};

    public SelectInventarisTableModel(List<ViewInventaris> listInventaris) {
        this.listInventaris = listInventaris;
    }

    public SelectInventarisTableModel() {
    }

    public int getRowCount() {
        return listInventaris.size();
    }

    public int getColumnCount() {
        return 4;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0:
                return listInventaris.get(rowIndex).getInventaris_id();
            case 1:
                return listInventaris.get(rowIndex).getJudul();
            case 2:
                return listInventaris.get(rowIndex).getIsbn();
            case 3:
                return listInventaris.get(rowIndex).getPengarang();
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        return header[column];
    }
}
